﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace vncom
{
    public class DataServices
    {
        public static string GetSetting(string username, string settingKey)
        {
            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@username", SqlDbType.NVarChar);
            p1.Value = username;
            listPara.Add(p1);
            SqlParameter p2 = new SqlParameter("@settingkey", SqlDbType.VarChar);
            p2.Value = settingKey;
            listPara.Add(p2);
            SqlParameter resultProc = new SqlParameter("@outvalue", SqlDbType.VarChar);
            resultProc.Value = "0";
            var cmd = DataProvider.OutPutProc("GetSetting", listPara, resultProc);
            return resultProc.Value.ToString();
        }

        public static string EditSetting(string username, string settingKey, string newValue)
        {
            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@username", SqlDbType.NVarChar);
            p1.Value = username;
            listPara.Add(p1);
            SqlParameter p2 = new SqlParameter("@settingkey", SqlDbType.VarChar);
            p2.Value = settingKey;
            listPara.Add(p2);

            SqlParameter p3 = new SqlParameter("@newvalue", SqlDbType.VarChar);
            p3.Value = "0";
            listPara.Add(p3);

            SqlParameter resultProc = new SqlParameter("@result", SqlDbType.VarChar);
            resultProc.Value = "0";
            var cmd = DataProvider.OutPutProc("EditSetting", listPara, resultProc);
            return resultProc.Value.ToString();
        }

        public static void NewSetting(string username, string settingKey, string value)
        {
            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@username", SqlDbType.NVarChar);
            p1.Value = username;
            listPara.Add(p1);
            SqlParameter p2 = new SqlParameter("@settingkey", SqlDbType.VarChar);
            p2.Value = settingKey;
            listPara.Add(p2);

            SqlParameter p3 = new SqlParameter("@value", SqlDbType.VarChar);
            p3.Value = value;
            listPara.Add(p3);

            DataProvider.NonqueryProc("NewSetting", listPara);
        }

        public static Dictionary<string,string> GetBannerSetting(string username)
        {
            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@username", SqlDbType.NVarChar);
            p1.Value = username;
            listPara.Add(p1);
            var dataset = DataProvider.SelectStoreProcedure("GetBannerSetting", listPara);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (DataRow item in dataset.Rows)
            {
                var key = item["SettingKey"].ToString();
                var value = item["Value"].ToString();
                dic.Add(key, value);
            }
            return dic;
        }

        public static List<string> GetBannerImage(string username)
        {
            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@username", SqlDbType.NVarChar);
            p1.Value = username;
            listPara.Add(p1);
            var dataset = DataProvider.SelectStoreProcedure("GetBannerImage", listPara);
            List<string> list = new List<string>();
            foreach (DataRow item in dataset.Rows)
            {
                var url = "/Memberpictures/" + item["Url"].ToString();
                list.Add(url);
            }
            return list;
        }


    }
}